package com.panopset.io;

import static com.panopset.Util.log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import com.panopset.Util;

/**
 * Properties file.
 *
 * @author Karl Dinwiddie
 *
 */
public final class PropertiesFile {

    /**
     * File.
     */
    private final File f;

    /**
     * Properties.
     */
    private Properties props;

    /**
     * @param fileName
     *            File name.
     */
    public PropertiesFile(final String fileName) {
        this(new File(fileName));
    }

    /**
     * @param file
     *            File.
     */
    public PropertiesFile(final File file) {
        f = file;
    }

    /**
     * @param key
     *            Key.
     * @return Value.
     */
    public synchronized String get(final String key) {
        return getProps().getProperty(key);
    }

    /**
     * @param key
     *            Key.
     * @param value
     *            Value.
     */
    public synchronized void put(final String key, final String value) {
        getProps().put(key, value);
        flush();
    }

    /**
     * Save to file.
     */
    public void flush() {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            if (!f.exists()) {
                File p = f.getParentFile();
                if (!p.exists() && !p.mkdirs()) {
                    log(Util.x(""));
                }
            }
            fw = new FileWriter(f);
            bw = new BufferedWriter(fw);
            getProps().store(bw, com.panopset.Util.TIMESTAMP_FORMAT
                    .format(new Date()));
        } catch (final Exception ex) {
            log(ex);
        } finally {
            try {
                if (bw != null) {
                    bw.flush();
                    bw.close();
                }
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e) {
                log(e);
            }
        }
    }

    /**
     * @return Properties.
     */
    private Properties getProps() {
        if (props == null) {
            props = new Properties();
            FileReader fr = null;
            BufferedReader br = null;
            if (f.exists()) {
                try {
                    fr = new FileReader(f);
                    br = new BufferedReader(fr);
                    props.load(br);
                } catch (Exception e) {
                    log(e);
                } finally {
                    try {
                        if (br != null) {
                            br.close();
                        }
                        if (fr != null) {
                            fr.close();
                        }
                    } catch (IOException e) {
                        log(e);
                    }
                }
            }
        }
        return props;
    }
}
